import { ProFormOptions } from '@/components/pro-form/type'
import { BorrowModel } from '@/api/model/borrow-model'
import { Ref } from 'vue'
import { optionsType } from '~/types/form'

type Props = {
  handleType: Ref<'add' | 'edit' | 'check'>
  typeOptions: Ref<any>
  typeLoading: Ref<boolean>
  orgOptions: Ref<any>
  orgFilterOptions: Ref<any>
  orgLoading: Ref<boolean>
}

export const useBorrowProForm = ({
  handleType,
  typeOptions,
  typeLoading,
  orgOptions,
  orgFilterOptions,
  orgLoading
}: Props) => {
  const [borrowForm, resetBorrowForm] = useProRef<Partial<BorrowModel>>({})

  const borrowFormOptions = computed<ProFormOptions>(() => {
    const list = [
      {
        formType: 'input',
        key: 'borrowName',
        label: '借用名称'
      },
      {
        formType: 'input',
        key: 'borrowCode',
        label: '借用编号',
        disabled: true
      },
      {
        formType: 'input',
        key: 'assetCode',
        label: '资产编号',
        disabled: handleType.value === 'edit'
      },
      {
        formType: 'input',
        label: '资产名称',
        key: 'assetName',
        disabled: true
      },
      {
        formType: 'select',
        key: 'typeId',
        label: '资产类型',
        options: typeOptions.value,
        loading: typeLoading.value,
        disabled: true
      },
      {
        formType: 'select',
        key: 'borrowInStatus',
        label: '借入状态',
        disabled: true,
        options: useDictData('STATUS').value
      },
      {
        formType: 'select',
        key: 'borrowOutStatus',
        label: '借出状态',
        disabled: true,
        options: useDictData('STATUS').value
      },
      {
        formType: 'input',
        key: 'receivePerson',
        label: '申请人',
        disabled: true
      },
      // {
      //   formType: 'select',
      //   key: 'borrowInBranchId',
      //   label: '借入部门',
      //   options: orgOptions.value,
      //   loading: orgLoading.value
      // },
      {
        formType: 'select',
        key: 'borrowOutBranchId',
        label: '借出部门',
        options: orgFilterOptions.value,
        loading: orgLoading.value,
        disabled: handleType.value === 'edit'
      },
      // {
      //   formType: 'input',
      //   key: 'receiveBranch',
      //   label: '申请部门',
      //   disabled: true
      // },
      {
        formType: 'date',
        key: 'receiveDate',
        label: '申请日期',
        disabled: true
      },
      // {
      //   formType: 'select',
      //   key: 'borrowType',
      //   label: '借用类型',
      //   dictOptions: useDictData('ALLOCATE_TYPE').value,
      //   disabled: true
      // },
      {
        formType: 'input',
        key: 'receiveReason',
        label: '借用原因'
      },
      {
        formType: 'textarea',
        key: 'remark',
        label: '备注',
        gridItem: {
          span: 2
        },
        placeholder: '请输入备注',
        noRule: true
      },
      {
        formType: 'textarea',
        key: 'inReason',
        label: '借入批复',
        rows: 1,
        gridItem: {
          span: 2
        },
        noRule: true,
        disabled: true
      },
      {
        formType: 'textarea',
        key: 'outreason',
        label: '借出批复',
        rows: 1,
        gridItem: {
          span: 2
        },
        noRule: true,
        disabled: true
      }
    ] as optionsType
    return handleType.value === 'add'
      ? useSetOptions(list.filter((item) => !item.disabled))
      : useSetOptions(list)
  })

  return {
    borrowForm,
    borrowFormOptions,
    resetBorrowForm
  }
}
